﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KPO_lab_02
{
    class BinHash
    {
        //константы
        int HASH_MIN = (int) '0' + (int) '0';
        int HASH_MAX = (int)'z' + (int)'z' + 50;
        Table[] hashArray;

        private int countAdd;
        private int countFind;

        public int getCountAdd
        {
            get
            {
                return countAdd;
            }
        }

        public int getCountFind
        {
            get
            {
                return countFind;
            }
        } 

        public BinHash()
        {
            hashArray = new Table[HASH_MAX - HASH_MIN]; 
        }

        private int getHash(string s)
        {
            try
            {
                return ((int)s[0] + (int)s[s.Length - 1] + s.Length) - HASH_MIN;
            }
            catch(IndexOutOfRangeException) 
            {
                MessageBox.Show("Строка нулевой длины!");
                return 0;
            }
        }

        public bool addTreeVar(string sName)
        {
            countAdd++;
            int hash = getHash(sName);
            if (hashArray[hash] != null)
            {
                return hashArray[hash].addElCount(sName, ref countAdd);
            }
            else
            {
                hashArray[hash] = new Table(sName);
                return true;
            }
        }

        public bool getTreeVar(string sName)
        {
            countFind = 0;
            int hash = getHash(sName);
            if (hashArray[hash] == null)
                return false;
            else return hashArray[hash].findElCount(sName,ref countFind);
        }

        public void clear()
        {
            countAdd = 0;
            countFind = 0;
        }
     }
}
